package com.seven.controller;

import com.seven.exception.UserNotExistException;
import com.seven.handler.MyExceptionHandler;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author seven
 * @Date 2020/4/27 13:09
 **/
@Controller
public class UserController {
    @PostMapping("/user/login")
    public String login(@RequestParam("username")String username,
                        @RequestParam("password")String password,
                        HashMap<String,Object> map,
                        HttpSession session){
        //为了好异常
        if (username.equals("error")){
            throw new UserNotExistException();
        }
        //简单模拟
        if (!StringUtils.isEmpty(username)&&"123".equals(password)){
            //登陆成功，防止表单重复提交，可以重定向到主页
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }
        //登录失败
        map.put("msg","用户名或者密码错误！");
        return "index";
    }
}
